package URL_download;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class InetAddress {
	public static void main(String[] args) throws Exception {
		System.out.println(InetAddress.getByName("localhost"));
		URL url=new URL("http://localhost:80/test/hao.html");
		System.out.println(url.getFile());
		System.out.println(url.getPort());
		System.out.println(url.getHost());
		System.out.println(url.getProtocol());
		
		System.out.println("download file");
		download (url, "t://haohao/hao.html");
	}
	private static char[] getByName(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	static void download(URL url,String savefile) throws Exception{
		InputStream is=url.openStream();
		FileOutputStream fos=new FileOutputStream(savefile);
		
		byte []buff=new byte[1024];
		int len=0;
		while ((len=is.read(buff))!=-1){
			fos.write(buff, 0, len);
			fos.flush();
		}
		is.close();fos.close();
		System.out.println("download finished");
	}
}
